package com.app.twth.ui.preprocess.shipment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.app.twth.BR;
import com.app.twth.R;
import com.app.twth.app.AppRouter;
import com.app.twth.base.BaseCameraPhotoActivity;
import com.app.twth.bean.FilePathListDTO;
import com.app.twth.bean.PhotoBean;
import com.app.twth.databinding.ActivityPreprocessShipmentBinding;
import com.app.twth.ui.checkin.PhotoAdapter;
import com.app.twth.ui.preprocess.PreprocessQr;
import com.app.twth.ui.preprocess.PreprocessViewModel;
import com.app.twth.utils.ToastUtil;
import com.app.twth.utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2024/5/7.
 * Description:
 * Encoding: utf-8
 */
public class PreprocessShipmentActivity extends BaseCameraPhotoActivity<ActivityPreprocessShipmentBinding, PreprocessViewModel> {
    private String TAG = getClass().getSimpleName();
    private int type = 0;
    private int position = 0;
    private PreprocessQr preprocessQr;
    private List<PhotoBean> overallPaths = new ArrayList<>();
    private List<PhotoBean> trkPaths = new ArrayList<>();
    private List<PhotoBean> skuPaths = new ArrayList<>();
    private List<PhotoBean> damagePaths = new ArrayList<>();
    private PhotoAdapter overallAdapter, trkAdapter, skuAdapter, damageAdapter;

    @Override
    protected int initViewModelId() {
        return BR.viewModel;
    }

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getBundleExtra(AppRouter.K_EXTRA);
        preprocessQr = (PreprocessQr) bundle.getSerializable("PreprocessQr");
        if (preprocessQr == null) {
            return;
        }

        setResetGone();
        setNext(getString(R.string.submit));

        binding.tvCode.requestFocus();

        binding.tvTrackingNumber.setText(preprocessQr.getTrackingCode());
        binding.tvSkuQuantity.setText(preprocessQr.getBarcode());

        overallPaths.add(new PhotoBean());
        overallAdapter = new PhotoAdapter(this, overallPaths, 3, position -> {
            type = 0;
            this.position = position;
            requestCamera("overall");
        }, str -> {
            type = 0;
        });
        GridLayoutManager overallManager = new GridLayoutManager(this, 3);
        binding.rvOverall.setLayoutManager(overallManager);
        binding.rvOverall.setAdapter(overallAdapter);

        trkPaths.add(new PhotoBean());
        trkAdapter = new PhotoAdapter(this, trkPaths, 3, position -> {
            type = 1;
            this.position = position;
            requestCamera("trk");
        }, str -> {
            type = 1;
        });
        GridLayoutManager trkManager = new GridLayoutManager(this, 3);
        binding.rvTrk.setLayoutManager(trkManager);
        binding.rvTrk.setAdapter(trkAdapter);

        skuPaths.add(new PhotoBean());
        skuAdapter = new PhotoAdapter(this, skuPaths, 3, position -> {
            type = 2;
            this.position = position;
            requestCamera("sku");
        }, str -> {
            type = 2;
        });
        GridLayoutManager skuManager = new GridLayoutManager(this, 3);
        binding.rvSku.setLayoutManager(skuManager);
        binding.rvSku.setAdapter(skuAdapter);

        damagePaths.add(new PhotoBean());
        damageAdapter = new PhotoAdapter(this, damagePaths, 3, position -> {
            type = 3;
            this.position = position;
            requestCamera("damaged");
        }, str -> {
            type = 3;
        });
        GridLayoutManager damageManager = new GridLayoutManager(this, 3);
        binding.rvDamaged.setLayoutManager(damageManager);
        binding.rvDamaged.setAdapter(damageAdapter);
    }

    @Override
    protected void initListener() {
        viewModel.submitLiveData.observe(this, code -> {
            Intent intent = new Intent();
            setResult(RESULT_OK, intent);
            finish();
        });
        viewModel.errorResultCode.observe(this, code -> {
            showHint((String) code);
        });
        viewModel.fileUploadMutableLiveData.observe(this, data -> {
            Log.e(TAG, "type():" + data.getType() + ",position:" + data.getPosition());
            switch (data.getType()) {
                case 0:
                    if (overallPaths.size() > data.getPosition()) {
                        PhotoBean photoBean = overallPaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
                case 1:
                    if (trkPaths.size() > data.getPosition()) {
                        PhotoBean photoBean = trkPaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
                case 2:
                    if (skuPaths.size() > data.getPosition()) {
                        PhotoBean photoBean = skuPaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
                case 3:
                    if (damagePaths.size() > data.getPosition()) {
                        PhotoBean photoBean = damagePaths.get(data.getPosition());
                        photoBean.setUrlImg(data.getPath());
                    }
                    break;
            }

            if (data.isSubmit()) {
                submitData();
            }
        });

        viewModel.isLoading.observe(this, b -> {
            if (b.isShow()) {
                showProgressDialog(b.getMsg());
            } else {
                dismissProgressDialog();
            }
        });
    }

    @Override
    protected String setTitle() {
        return "Preprocess(Submit)";
    }

    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_reset:
                break;
            case R.id.tv_next:
                if (System.currentTimeMillis() - timeMillis < spaceTime) {
                    return;
                }
                timeMillis = System.currentTimeMillis();
                submitData();
                break;
        }
    }

    @Override
    protected void getImgPath(String pathImage) {
        setPath(type, pathImage);
    }

    private void setPath(int type, String pathImage) {
        Log.e(TAG, "setPath>type:" + type + ",pathImage:" + pathImage);

        if (!pathImage.contains("http")) {//选择的图片
            String fileName = "";
            switch (type) {
                case 0:
                    fileName = "overall";
                    break;
                case 1:
                    fileName = "trk";
                    break;
                case 2:
                    fileName = "sku";
                    break;
                case 3:
                    fileName = "damaged";
                    break;
            }
            Log.e(TAG, "setPath>type:" + type + ",fileName:" + fileName);
            viewModel.fileUpload(preprocessQr.getTrackingCode(), fileName, pathImage, type, 0, false, false);
        }

        PhotoBean imgUrl = new PhotoBean();
        imgUrl.setPathImg(pathImage);
        imgUrl.setUrlImg(pathImage);
        switch (type) {
            case 0:
                if (overallPaths.size() > position) {
                    overallPaths.remove(position);
                    overallPaths.add(position, imgUrl);
                }
                if (overallPaths.size() < 1) {
                    if (!Utils.isEmpty(overallPaths.get(overallPaths.size() - 1).getPathImg())) {
                        trkPaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : overallPaths) {
                    Log.e(TAG, img.toString());
                }
                if (overallAdapter != null) {
                    overallAdapter.setData(overallPaths);
                }
                break;
            case 1:
                if (trkPaths.size() > position) {
                    trkPaths.remove(position);
                    trkPaths.add(position, imgUrl);
                }
                if (trkPaths.size() < 1) {
                    if (!Utils.isEmpty(trkPaths.get(trkPaths.size() - 1).getPathImg())) {
                        trkPaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : trkPaths) {
                    Log.e(TAG, img.toString());
                }
                if (trkAdapter != null) {
                    trkAdapter.setData(trkPaths);
                }
                break;
            case 2:
                if (skuPaths.size() > position) {
                    skuPaths.remove(position);
                    skuPaths.add(position, imgUrl);
                }
                if (skuPaths.size() < 1) {
                    if (!Utils.isEmpty(skuPaths.get(skuPaths.size() - 1).getPathImg())) {
                        skuPaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : skuPaths) {
                    Log.e(TAG, img.toString());
                }
                if (skuAdapter != null) {
                    skuAdapter.setData(skuPaths);
                }
                break;
            case 3:
                if (damagePaths.size() > position) {
                    damagePaths.remove(position);
                    damagePaths.add(position, imgUrl);
                }
                if (damagePaths.size() < 1) {
                    if (!Utils.isEmpty(damagePaths.get(damagePaths.size() - 1).getPathImg())) {
                        damagePaths.add(new PhotoBean());
                    }
                }
                for (PhotoBean img : damagePaths) {
                    Log.e(TAG, img.toString());
                }
                if (damageAdapter != null) {
                    damageAdapter.setData(damagePaths);
                }
                break;
        }
    }


    private void submitData() {
        String code = binding.tvCode.getText().toString();
        if (Utils.isEmpty(code)) {
            ToastUtil.showToast("Please input inbound lpn");
            return;
        }
        List<String> overallPathList = new ArrayList<>();
        if (overallPaths.size() > 0) {
            for (int i = 0; i < overallPaths.size(); i++) {
                PhotoBean photoBean = overallPaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(preprocessQr.getTrackingCode(), "overall", s, 0, position, true, true);
                        return;
                    }
                    overallPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(preprocessQr.getTrackingCode(), "overall", photoBean.getPathImg(), 0, position, true, true);
                    return;
                }
            }
        }
        if (overallPathList.size() == 0) {
            ToastUtil.showToast("Please add overall photos");
            return;
        }

        List<String> trkPathList = new ArrayList<>();
        if (trkPaths.size() > 0) {
            for (int i = 0; i < trkPaths.size(); i++) {
                PhotoBean photoBean = trkPaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(preprocessQr.getTrackingCode(), "trk", s, 1, position, true, true);
                        return;
                    }
                    trkPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(preprocessQr.getTrackingCode(), "trk", photoBean.getPathImg(), 1, position, true, true);
                    return;
                }
            }
        }
        if (trkPathList.size() == 0) {
            ToastUtil.showToast("Please add trk photos");
            return;
        }

        List<String> skuPathList = new ArrayList<>();
        if (skuPaths.size() > 0) {
            for (int i = 0; i < skuPaths.size(); i++) {
                PhotoBean photoBean = skuPaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(preprocessQr.getTrackingCode(), "sku", s, 2, position, true, true);
                        return;
                    }
                    skuPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(preprocessQr.getTrackingCode(), "sku", photoBean.getPathImg(), 2, position, true, true);
                    return;
                }
            }
        }
        if (skuPathList.size() == 0) {
            ToastUtil.showToast("Please add sku photos");
            return;
        }


        List<String> damagedPathList = new ArrayList<>();
        if (damagePaths.size() > 0) {
            for (int i = 0; i < damagePaths.size(); i++) {
                PhotoBean photoBean = damagePaths.get(i);
                String s = photoBean.getUrlImg();
                Log.e(TAG, "s:" + photoBean.toString());
                if (!Utils.isEmpty(s)) {
                    if (!s.contains("http")) {
                        position = i;
                        viewModel.fileUpload(preprocessQr.getTrackingCode(), "damaged", s, 3, position, true, true);
                        return;
                    }
                    damagedPathList.add(s);
                } else if (!Utils.isEmpty(photoBean.getPathImg())) {
                    position = i;
                    viewModel.fileUpload(preprocessQr.getTrackingCode(), "damaged", photoBean.getPathImg(), 3, position, true, true);
                    return;
                }
            }
        }

        List<FilePathListDTO> filePathList = new ArrayList<>();

        FilePathListDTO overallFilePathListDTO = new FilePathListDTO();
        overallFilePathListDTO.setName("overall");
        overallFilePathListDTO.setPathList(overallPathList);
        filePathList.add(overallFilePathListDTO);

        FilePathListDTO trkFilePathListDTO = new FilePathListDTO();
        trkFilePathListDTO.setName("trk");
        trkFilePathListDTO.setPathList(trkPathList);
        filePathList.add(trkFilePathListDTO);

        FilePathListDTO skuFilePathListDTO = new FilePathListDTO();
        skuFilePathListDTO.setName("sku");
        skuFilePathListDTO.setPathList(skuPathList);
        filePathList.add(skuFilePathListDTO);


        if (damagedPathList.size() > 0) {
            FilePathListDTO damagedFilePathListDTO = new FilePathListDTO();
            damagedFilePathListDTO.setName("damaged");
            damagedFilePathListDTO.setPathList(damagedPathList);
            filePathList.add(damagedFilePathListDTO);
            preprocessQr.setPackageStatus("2");
        } else {
            preprocessQr.setPackageStatus("1");
        }
        preprocessQr.setIbLpn(code);
        preprocessQr.setFilePathList(filePathList);
        viewModel.returnPreprocessSubmit(preprocessQr);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        initScan();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unScan();
    }

    @Override
    public void getScanninCode() {
        super.getScanninCode();
        binding.tvCode.setText(barcodeStr);
    }

}
